package com.lyy.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 俞伟
 * @version 1.0
 * @date 2020/10/24
 * @description com.lyy.common.utils
 */
@Slf4j
public class RulesUtils {

    /**
     * 验证是否是图片
     *
     * @param name
     * @return
     */
    public static boolean checkIsImage(String name) {
        if (StringUtils.isBlank(name)) {
            return false;
        }
        String reg = ".+(.JPEG|.jpeg|.JPG|.jpg|.BMP|.bmp|.PNG|.png)$";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(name.toLowerCase());
        return matcher.find();
    }

    /**
     * 验证手机号码是否正确
     *
     * @param name
     * @return
     */
    public static boolean checkIsMobiles(String mobiles) {
        if (StringUtils.isBlank(mobiles)) {
            return false;
        }
        Pattern pattern = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
        Matcher matcher = pattern.matcher(mobiles);
        return matcher.matches();
    }

    /**
     * 判断是否是正整数
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String number) {
        if (StringUtils.isBlank(number)) {
            return false;
        }
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(number).matches();
    }

    /**
     * 判断是否是数字(可以判断正负、整数小数)
     *
     * @param str
     * @return
     */
    public static boolean isNumber(String string) {
        if (string == null) {
            return false;
        }
        Pattern pattern = Pattern.compile("^-?\\d+(\\.\\d+)?$");
        return pattern.matcher(string).matches();
    }

    /**
     * 验证身份证号码是否正确
     *
     * @param idCard
     * @return boolean
     */
    public static boolean getValid(String IDNumber) {
        if (null == IDNumber || "".equals(IDNumber)) {
            return false;
        }
        // 定义判别用户身份证号的正则表达式（15位或者18位，最后一位可以为字母）
        String regularExpression = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|"
                + "(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";
        boolean matches = IDNumber.matches(regularExpression);
        //判断第18位校验值
        if (matches) {
            if (IDNumber.length() == 18) {
                try {
                    char[] charArray = IDNumber.toCharArray();
                    //前十七位加权因子
                    int[] idCardWi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
                    //这是除以11后，可能产生的11位余数对应的验证码
                    String[] idCardY = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
                    int sum = 0;
                    for (int i = 0; i < idCardWi.length; i++) {
                        int current = Integer.parseInt(String.valueOf(charArray[i]));
                        int count = current * idCardWi[i];
                        sum += count;
                    }
                    char idCardLast = charArray[17];
                    int idCardMod = sum % 11;
                    if (idCardY[idCardMod].toUpperCase().equals(String.valueOf(idCardLast).toUpperCase())) {
                        return true;
                    } else {
                        log.info("身份证最后一位::{},错误,正确的应该是::{}", String.valueOf(idCardLast).toUpperCase(), idCardY[idCardMod].toUpperCase());
                        return false;
                    }
                } catch (Exception e) {
                    log.info("验证身份证异常::{}", e.getMessage());
                    return false;
                }
            }
        }
        return matches;
    }

    /**
     * 隐藏身份证号码
     *
     * @param idCard
     * @return String
     */
    public static String getHide(String idCard) {
        return idCard.replaceAll("(\\d{2})\\d{14}(\\w{2})", "$1**********$2");
    }


    /**
     * 根据身份证号码获取性别
     *
     * @param idCard
     * @return String
     */
    public static String getSexString(String idCard) {
        if (Integer.parseInt(idCard.substring(16).substring(0, 1)) % 2 == 0) {// 判断性别
            return "女";
        } else {
            return "男";
        }
    }

    /**
     * 根据身份证号码获取性别
     *
     * @param idCard
     * @return int 1男，2女
     */
    public static String getSexInt(String idCard) {
        if (Integer.parseInt(idCard.substring(16).substring(0, 1)) % 2 == 0) {// 判断性别
            return "2";
        } else {
            return "1";
        }
    }

    /**
     * 根据身份证号码获取生日 yyyy-MM-dd
     *
     * @param idCard
     * @return String
     */
    public static String getBirthdayString(String idCard) {
        String birth = idCard.substring(6, 14);
        return birth.substring(0, 4) + "-" + birth.substring(4, 6) + "-" + birth.substring(6, 8);
    }

    /**
     * 判断字符串是否json字符串
     *
     * @param str
     * @return
     */
    public static boolean isJsonStr(String str) {
        if (JSON.isValid(str)) {
            try {
                JSON.parseObject(str);
                return true;
            } catch (Exception e) {
                return false;
            }
        }
        return false;
    }

    /**
     * 判断字符串是否json数组
     *
     * @param str
     * @return
     */
    public static boolean isJsonArr(String str) {
        if (JSON.isValidArray(str)) {
            try {
                JSON.parseArray(str);
                return true;
            } catch (Exception e) {
                return false;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println(checkIsMobiles("18019996045"));
    }
}
